BlogsContact

Computing

2025-02-09 Check SSL Certificate valid

So that I do not have to rely on a third party to notify me when my certificates have expired I am using a shell script on my server that runs in a cron job to email me an alert of the status of the certificate

The script is a nodejs script that runs as a shell script and should be self explanatory. It uses the nodejs modules email and ssl-checker

#!/usr/local/bin/node
const path = require("path");
const emailer = require(path.join(__dirname, "..", "modules/email"));
const sslChecker = require("ssl-checker")
const uri = 'mydomain.co.uk'
const dayswarning = 20

function sendMessage(subject, message) {
  emailer.sendAdminEmail('message', {subject: subject, message: message}, 'myemail@emailserver.com', function(error, success) {
    if (error) {
      console.log('Error returned from sending email: ' + error.message);
    }
  });
}

sslChecker(uri, 'GET', 443).then(result => {
    if (result.daysRemaining < dayswarning) {
        sendMessage("Certificate expires in " + result.daysRemaining + " days", uri + " about to expire in " + result.daysRemaining + " days")
    } else {
        sendMessage("Certificate OK " + result.daysRemaining + " days remaining", uri + " has " + result.daysRemaining + " days remaining")
    }
});
© Jeremy Smith  Privacy Policy